package com.moter.demo01.juc.alllocks.deadlock;

import java.util.concurrent.TimeUnit;

public class DeadLoopWait {
    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();
        new Thread(()->{
            synchronized (a){
                System.out.println(Thread.currentThread().getName()+"\t拿到A锁,尝试拿B锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b){
                    System.out.println(Thread.currentThread().getName()+"\t拿到B锁");
                }
            }
        }).start();
        new Thread(()->{
            synchronized (b){
                System.out.println(Thread.currentThread().getName()+"\t拿到B锁,尝试拿A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println(Thread.currentThread().getName()+"\t拿到A锁");
                }
            }
        }).start();
        //jsp -l    jstack 进程号调试
        //Found one Java-level deadlock: 发现一个死锁
        //=============================
        //"Thread-1":
        //  waiting to lock monitor 0x0000024b1aec7d08 (object 0x000000076b19b850, a java.lang.Object),
        //  which is held by "Thread-0"
        //"Thread-0":
        //  waiting to lock monitor 0x0000024b1aeca648 (object 0x000000076b19b860, a java.lang.Object),
        //  which is held by "Thread-1"
    }
}
